﻿using CYTS.Aoyou.Framework.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace CYTS.Aoyou.Framework.Web
{
    [Obsolete]
    public class AoyouController : Controller
    {
        [Obsolete]
        protected JsonResult AoyouJson(Object data)
        {
            return Json(new { resultkey = "success", resultvalue = data }, JsonRequestBehavior.AllowGet);
        }

        [Obsolete]
        protected JsonResult AoyouJson(Object data, JsonRequestBehavior behavior)
        {
            return Json(new { resultkey = "success", resultvalue = data }, behavior);
        }

        [Obsolete]
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //TODO...权限验证
            base.OnActionExecuting(filterContext);
        }

        [Obsolete]
        protected override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            if (filterContext.Result.GetType() == typeof(JsonResult)) //如果返回是Json格式的数据
            {
                //自定义返回JsonResult的数据结构
                Object myResult = new { 
                    code = "success",
                    data = ((JsonResult)filterContext.Result).Data,
                    error = new { },
                };

                filterContext.Result = new JsonResult() { Data = myResult, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
                filterContext.Result.ExecuteResult(filterContext.Controller.ControllerContext);
                filterContext.Cancel = true;
            }
            else if (filterContext.Result.GetType() == typeof(ActionResult))
            {
                //TODO...
            }
        }

        /// <summary>
        /// 异常拦截器
        /// </summary>
        /// <param name="filterContext"></param>
        [Obsolete]
        protected override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.Result.GetType() == typeof(EmptyResult)) //如果返回是Json格式的数据
            {
                ExceptionConfig config = ConfigFactory.GetConfig<ExceptionConfig>();

                Object myResult = new Object();
                if (config.DebugMode) //调试模式
                {
                    //自定义返回JsonResult的数据结构
                    myResult = new
                    {
                        code = "failed",
                        data = "获取数据失败，请联系管理员进行解决！",
                        error = new { msg = filterContext.Exception.Message, info = filterContext.Exception.ToString() }
                    };
                }
                else //运行模式
                {
                    //自定义返回JsonResult的数据结构
                    myResult = new
                    {
                        code = "failed",
                        data = "获取数据失败，请联系管理员进行解决！",
                        error = new { msg = "", info = "" }
                    };
                }

                filterContext.Result = new JsonResult() { Data = myResult, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
                filterContext.ExceptionHandled = true;
            }
        }
    }
}
